library(fixest)
rm(list=setdiff(ls(), "twd")) 


data<-readRDS(paste(twd,"data/made_data/vreg_wgeos_race.rds",sep=""))


## In person 20
data$in_person_primary_20<-ifelse(data$voting_method_200303 == "P",1,0)
## Abs early 20
data$abs_early_primary_20<-ifelse(data$voting_method_200303 %in% c("A","E"),1,0)
## In person 18
data$in_person_general_18<-ifelse(data$voting_method_181106 == "P",1,0)
## Abs early 18
data$abs_early_general_18<-ifelse(data$voting_method_181106 %in% c("A","E"),1,0)
## Take First Difference
data$diff_in_person<-data$in_person_primary_20-data$in_person_general_18
data$diff_abs_early<-data$abs_early_primary_20-data$abs_early_general_18
### get the minimum of the distance to a consolidated (including super sites)
data$distance_realized_min<-apply(cbind(data$distance_realized,data$distancegov1,data$distancegov2),1,min,na.rm=T)
## Difference between assigned and realized distances (in logs and levels)
data$ln_dist_change<-ifelse(data$moved==1,log(data$distance_realized_min)-log(data$distance_assigned),0)
data$lev_dist_change<-ifelse(data$moved==1,data$distance_realized_min-data$distance_assigned,0)
### Difference between assigned and realized size # of assigned voters (in logs and levels)
data$ln_diff_N<-log(data$realized_N)-log(data$assigned_N)
data$lev_diff_N<-data$realized_N-data$assigned_N


ft5<-feols(diff_in_person~moved+consolidated+ln_dist_change+ln_diff_N+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early,data=data,cluster~polling_place_text_name)



pdf(paste(twd,"figs/marginal_effects_main.pdf",sep=""),12,6)

newdata_diff_N<-sort(unique(data$ln_diff_N))
newdata_diff_N<-newdata_diff_N[newdata_diff_N >0]
names(newdata_diff_N)<-c("ln_diff_N")
newdata_1<-data.frame(newdata_diff_N,rep(0, length(newdata_diff_N)), rep(1,length(newdata_diff_N)),rep(1,length(newdata_diff_N)), 
                      rep(0,length(newdata_diff_N)),rep(0,length(newdata_diff_N)),rep(0,length(newdata_diff_N)) )

names(newdata_1)<-c("ln_diff_N","ln_dist_change","moved","consolidated","dist_damage_min","dist_power_min","diff_abs_early")

se_fitt_N<-predict(ft5,newdata=data.frame(newdata_1),se.fit=T)$se.fit

effect_diff_N<-predict(ft5,newdata=data.frame(newdata_1)) -coef(ft5)[1]

par(mar=c(5,5,3,1))
par(mfrow=c(1,2))
hist(data[data$consolidated==1,"ln_diff_N"],axes=F,xlab="",border=F,col="light grey",main="Effect of Precinct Size for Consolidated Voters",ylab="Effect on In-Person Turnout")
par(new=T)

plot(newdata_1$ln_diff_N , effect_diff_N ,ylim=c(-.3,.1),axes=F,type="n",xlab="ln(Size Realized) - ln(Size Assigned)",ylab="" )
grid()
lines(newdata_1$ln_diff_N , effect_diff_N,lwd=2)
lines(newdata_1$ln_diff_N,effect_diff_N-1.96*se_fitt_N,lty=2)
lines(newdata_1$ln_diff_N,effect_diff_N+1.96*se_fitt_N,lty=2)
axis(2)
axis(1)
abline(h=0,lty=4,col="dark red",lwd=2)



newdata_diff_dist<-sort(unique(data$ln_dist_change))
newdata_2<-data.frame(rep(0,length(newdata_diff_dist)),newdata_diff_dist, rep(1,length(newdata_diff_dist)),rep(1,length(newdata_diff_dist)), 
                      rep(0,length(newdata_diff_dist)),rep(0,length(newdata_diff_dist)),rep(0,length(newdata_diff_dist)) )


names(newdata_2)<-c("ln_diff_N","ln_dist_change","moved","consolidated","dist_damage_min","dist_power_min","diff_abs_early")


se_diff_change<-predict(ft5,newdata=data.frame(newdata_2),se.fit=T)$se.fit

effect_diff_change<-predict(ft5,newdata=data.frame(newdata_2)) -coef(ft5)[1]





hist(data[data$moved==1 & data$consolidated==1,"ln_dist_change"],axes=F,xlab="",border=F,col="light grey",breaks=40,xlim=range(newdata_diff_dist),
     main="Effect of Travel Distance for Reprecincted Voters",ylab="Effect on In Person Turnout")
par(new=T)


plot(newdata_2$ln_dist_change , effect_diff_change,axes=F,type="n",xlab="ln(Distance Realized) - ln(Distance Assigned)",ylab="", ylim=c(-.25,.1))
grid()
lines(newdata_2$ln_dist_change , effect_diff_change,lwd=2)
lines(newdata_2$ln_dist_change,effect_diff_change-1.96*se_diff_change,lty=2,lwd=2)
lines(newdata_2$ln_dist_change,effect_diff_change+1.96*se_diff_change,lty=2,lwd=2)
axis(2)
axis(1,seq(-2,6,by=1))
abline(h=0,lty=4,col="dark red",lwd=2)

dev.off()